package com.icss.ah.shop.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.shop.bean.HomeApplicationBean;

@Service
@Transactional
public class ShopManageService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 查询店铺名称是否可用
	 * @param shopName
	 * @return
	 */
	public boolean checkShopNameExist(String shopName) {
		boolean result = false;
		try{
			HashMap info = new HashMap();
			info.put("shopName", shopName);
			Integer count = sqlSessionTemplate.selectOne("ShopManageBean.checkShopNameExist", info);
			infolog.info("ShopManageService|checkShopNameExist|count:"+count);
			if(count>0){
				result = false;
			} else {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopManageService|checkShopNameExist", e);
		}
		return result;
	}

	/**
	 * 查询店铺申请信息
	 * @param map
	 * @return
	 */
	public HashMap queryAccountMap(HashMap<String, Object> map) {
		HashMap reMap = new HashMap();
		try {
			reMap = (HashMap)sqlSessionTemplate.selectOne("ShopManageBean.queryAccountMap",map);
			infolog.info("ShopManageService|queryAccountMap|reMap:"+reMap);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|queryAccountMap", ex);
		} 
		return reMap = reMap == null ? new HashMap() : reMap;
	}

	/**
	 * 查询店铺信息
	 * @param map
	 * @return
	 */
	public HashMap queryShopMap(HashMap<String, Object> map) {
		HashMap reMap = new HashMap();
		try {
			reMap = (HashMap)sqlSessionTemplate.selectOne("ShopManageBean.queryShopMap",map);
			infolog.info("ShopManageService|queryShopMap|reMap:"+reMap);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|queryShopMap", ex);
		} 
		return reMap = reMap == null ? new HashMap() : reMap;
	}

	/**
	 * 保存新增店铺信息
	 * @param map
	 * @return
	 */
	public boolean insertShop(HashMap<String, Object> map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.insert("ShopManageBean.insertShop",map);
			reValue = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|insertShop", ex);
		} 
		return reValue;
	}

	/**
	 * 更新店铺信息表信息
	 * @param map
	 * @return
	 */
	public boolean updateShop(HashMap<String, Object> map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("ShopManageBean.updateShop",map);
			reValue = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|updateShop", ex);
		} 
		return reValue;
	}

	/**
	 * 更新店铺登录信息表中店铺申请信息
	 * @param map
	 * @return
	 */
	public boolean updateAccountShop(HashMap<String, Object> map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.update("ShopManageBean.updateAccountShop",map);
			reValue = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|updateAccountShop", ex);
		} 
		return reValue;
	}

	/**
	 * 获取经营区域数据
	 */
	public List<SyncSelectTree> getTree(SyncSelectTree selectTree) {
		String id = selectTree.getId();//id 异步加载展开树时传到后台的id
		String ifAll = selectTree.getIfAll();//是否一次性展示所有数据，1是 0否
		List list = new ArrayList();
		try {
			if (ifAll != null && !"".equals(ifAll)) {
				if ("0".equals(ifAll)) {//展开树时加载数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("ShopManageBean.getTree",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("ShopManageBean.getTreeChild",selectTree);
					}
				}else{//一次加载所有数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("ShopManageBean.getTreeAll",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("ShopManageBean.getTreeChild",selectTree);
					}
				}
			}
			infolog.info("ShopManageService|getTree|list:"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopManageService|getTree", e);
		}
		return list == null ? new ArrayList() : list;
	}
	
	/**
	 * 获取店铺已勾选经营区域信息
	 * @param map
	 * @return
	 */
	public List<Map> queryCheckList(HashMap map) {
		List<Map> list = new ArrayList<Map>();
		try{
			list = sqlSessionTemplate.selectList("ShopManageBean.queryCheckList", map);
			infolog.info("ShopManageService|queryCheckList|list:"+list);
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|queryCheckList", ex);
		} 
		return list == null ? new ArrayList<Map>() : list;
	}

	/**
	 * 删除原店铺经营区域信息
	 * @param map
	 * @return
	 */
	public boolean deleteShopArea(HashMap<String, Object> map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.delete("ShopManageBean.deleteShopArea",map);
			reValue = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|deleteShopArea", ex);
		} 
		return reValue;
	}

	/**
	 * 添加经营区域信息
	 * @param map
	 */
	public boolean insertShopArea(HashMap<String, Object> map) {
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.insert("ShopManageBean.insertShopArea",map);
			reValue = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|insertShopArea", ex);
		} 
		return reValue;
	}

	/**
	 * 申请首页展示
	 * @param homeApplicationBean 
	 * @return 
	 */
	public boolean applyHomeDisplay(HomeApplicationBean homeApplicationBean) {
		boolean result = false;
		try {
			sqlSessionTemplate.insert("ShopManageBean.applyHomeDisplay",homeApplicationBean);
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			errlog.error("ShopManageService|applyHomeDisplay", ex);
		} 
		return result;
		
	}

	/**
	 * 获取店铺申请首页展示信息
	 * @param map
	 * @return
	 */
	public HomeApplicationBean getHomeApplicationInfo(Map<String, String> map) {
		HomeApplicationBean homeApplicationBean = null;
		try {
			homeApplicationBean = sqlSessionTemplate.selectOne("ShopManageBean.getHomeApplicationInfo", map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopManageService|getHomeApplicationInfo", e);
		}
		
		return homeApplicationBean;
	}

	/**
	 * 修改宣传信息
	 * @param homeApplicationBean
	 * @return
	 */
	public boolean upDateApplyHomeDisplay(
			HomeApplicationBean homeApplicationBean) {
		boolean result=false;
		try {
			sqlSessionTemplate.update("ShopManageBean.upDateApplyHomeDisplay", homeApplicationBean);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopManageService|upDateApplyHomeDisplay", e);
		}
		return result;
	}

	/**
	 * 再次提交申请
	 * @param homeApplicationBean
	 * @return
	 */
	public boolean applyAgain(HomeApplicationBean homeApplicationBean) {
		boolean result=false;
		try {
			sqlSessionTemplate.update("ShopManageBean.applyAgain", homeApplicationBean);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopManageService|applyAgain", e);
		}
		return result;
	}

}
